import 'package:flame/extensions.dart';
import 'package:flame/game.dart';
import 'package:flame/gestures.dart';
import 'package:flame/palette.dart';

class MultitapAdvancedGame extends BaseGame with MultiTouchTapDetector, MultiTouchDragDetector {
  static final whitePaint = BasicPalette.white.paint();
  static final tapSize = Vector2.all(50);

  final Map<int, Rect> taps = {};

  Vector2? start;
  Vector2? end;
  Rect? panRect;

  @override
  void onTapDown(int pointerId, TapDownInfo event) {
    final position = event.eventPosition.game - tapSize / 2;
    taps[pointerId] = position.toPositionedRect(tapSize);
  }

  @override
  void onTapUp(int pointerId, TapUpInfo event) {
    taps.remove(pointerId);
  }

  @override
  void onTapCancel(int pointerId) {
    taps.remove(pointerId);
  }

  @override
  void onDragCancel(int pointerId) {
    end = null;
    start = null;
    panRect = null;
  }

  @override
  void onDragStart(int pointerId, DragStartInfo info) {
    end = null;
    start = info.eventPosition.game;
  }

  @override
  void onDragUpdate(int pointerId, DragUpdateInfo info) {
    end = info.eventPosition.game;
  }

  @override
  void onDragEnd(int pointerId, DragEndInfo info) {
    final start = this.start;
    final end = this.end;

    if (start != null && end != null) {
      panRect = start.toPositionedRect(end - start);
    }
  }

  @override
  void render(Canvas canvas) {
    final panRect = this.panRect;
    super.render(canvas);
    taps.values.forEach((element) {
      canvas.drawRect(element, whitePaint);
    });
    if (panRect != null) {
      canvas.drawRect(panRect, whitePaint);
    }
  }
}
